//
//  SelectMajorViewController.m
//  MyProject
//
//  Created by Pei on 16/7/8.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

/*
 Error = "";
 ErrorCode = "";
 SpecialtyList =     (
 {
 SpecialtyCode = DJYJCGC;
 SpecialtyName = "\U5730\U57fa\U4e0e\U57fa\U7840\U5de5\U7a0b";
 },
 {
 SpecialtyCode = FDAZ;
 SpecialtyName = "\U98ce\U7535\U5b89\U88c5";
 },
 {
 SpecialtyCode = FDJC;
 SpecialtyName = "\U98ce\U7535\U57fa\U7840";
 },
 {
 SpecialtyCode = GKYHAGC;
 SpecialtyName = "\U6e2f\U53e3\U4e0e\U6d77\U5cb8\U5de5\U7a0b";
 },
 {
 SpecialtyCode = GKYHDGC;
 SpecialtyName = "\U6e2f\U53e3\U4e0e\U822a\U9053\U5de5\U7a0b";
 },
 {
 SpecialtyCode = GLGC;
 SpecialtyName = "\U516c\U8def\U5de5\U7a0b";
 },
 {
 SpecialtyCode = "HNT?YZGJ";
 SpecialtyName = "\U6df7\U51dd\U571f\U3001\U9884\U5236\U6784\U4ef6";
 },
 {
 SpecialtyCode = QLGC;
 SpecialtyName = "\U6865\U6881\U5de5\U7a0b";
 },
 {
 SpecialtyCode = SZGC;
 SpecialtyName = "\U5e02\U653f\U5de5\U7a0b";
 },
 {
 SpecialtyCode = SDGC;
 SpecialtyName = "\U96a7\U9053\U5de5\U7a0b";
 },
 {
 SpecialtyCode = TLGC;
 SpecialtyName = "\U94c1\U8def\U5de5\U7a0b";
 }
 );
 Success = 1;
 
 */

#import "SelectMajorViewController.h"

@interface SelectMajorViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_dataArray;
    UITableView *_myTable;
    
    NSIndexPath *_lastIndexPath;
}

@end

@implementation SelectMajorViewController

- (void)viewDidLoad {
    self.navTitle = @"专业选择";
    [super viewDidLoad];
    
    _lastIndexPath = nil;
    
    [self initTableView];
    
    NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:Select_Major];
    if (tempArray != nil && tempArray.count > 0) {
        _dataArray = tempArray;
    }else{
        [self getData];
    }
}

- (void)initTableView
{
    _myTable = [[UITableView alloc] init];
    _myTable.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
    _myTable.delegate = self;
    _myTable.dataSource = self;
    [self.view addSubview:_myTable];
    
    _myTable.tableFooterView = [UIView new];
}

- (void)getData
{
    [[YMNetLib getInstance] connectWithAction:Get_All_Major_API method:@"GET" withContent:@"" withBody:nil withDelegate:self];
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
    {
        _dataArray = [body valueForKey:@"SpecialtyList"];
        [[NSUserDefaults standardUserDefaults] setObject:[body valueForKey:@"SpecialtyList"] forKey:Select_Major];
        [_myTable reloadData];
    }
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell_house_History";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.textColor = kRGB(205, 206, 207);
    cell.textLabel.font = kAppFont(Home_Small_Size+2);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [tempDic valueForKey:@"SpecialtyName"];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54.0f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger newRow = [indexPath row];
    NSInteger oldRow = -1;
    if (_lastIndexPath != nil)
    {
        oldRow = [_lastIndexPath row];
    }
    
    
    if (newRow != oldRow)
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                    indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        
        _lastIndexPath = indexPath;
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];
    
    
    if (self.delegateMajor && [self.delegateMajor respondsToSelector:@selector(passSelectMajor:)])
    {
        [self.delegateMajor passSelectMajor:tempDic];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
